local qinzheng = fk.CreateSkill({
    name = "sj6__qinzheng",
})

Fk:loadTranslationTable{
  ["sj6__qinzheng"] = "勤政",
  [":sj6__qinzheng"] = "出牌阶段开始时，你可以重铸所有牌。出牌阶段结束时，若你于此阶段内使用牌数大于你当前手牌数，你本回合下一个阶段改为出牌阶段。你发动此技能后，摸一张牌。",

  ["@sj6__qinzheng-phase"] = "勤政",
  ["#sj6__qinzheng"] = "勤政：你可以重铸所有牌",

  ["$sj6__qinzheng1"] = "据疆土为强富，制威福为尊贵。",
  ["$sj6__qinzheng2"] = "曜德义为荣显，永世胤为丰祚。",
}

qinzheng:addEffect(fk.EventPhaseStart,{
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qinzheng.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name = qinzheng.name,prompt = "#sj6__qinzheng"})
  end,
  on_use = function(self, event, target, player, data)
    player.room:recastCard(player:getCardIds("he"),player,qinzheng.name)
    if not player.dead then
      player:drawCards(1,qinzheng.name)
    end
  end,
})

qinzheng:addEffect(fk.EventPhaseEnd,{
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(qinzheng.name) and player.phase == Player.Play
    and player:getMark("@sj6__qinzheng-phase") > player:getHandcardNum()
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player,"sj6__qinzheng-turn",1)
  end,
})

qinzheng:addEffect(fk.EventPhaseChanging, {
  can_trigger = function(self, event, target, player, data)
    if player:getMark("sj6__qinzheng-turn") > 0 and data.phase > Player.RoundStart and data.phase < Player.NotActive then
      return player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player,"sj6__qinzheng-turn",0)
    room:sendLog{
      type = "#PhaseChanged",
      from = player.id,
      arg = Util.PhaseStrMapper(data.phase),
      arg2 = "phase_play",
    }
    data.phase = Player.Play
    player:drawCards(1,qinzheng.name)
  end,
})

qinzheng:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
  return target == player and player:hasSkill(qinzheng.name,true) and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player,"@sj6__qinzheng-phase")
  end,
})

qinzheng:addAcquireEffect(function (self, player, is_start)
  if player.phase == Player.Play then
    player.room:setPlayerMark(player,"@sj6__qinzheng-phase",
      #player.room.logic:getEventsOfScope(GameEvent.UseCard,999,function (e)
        return e.data.from == player
      end,Player.HistoryPhase))
  end
end)

return qinzheng